<?php
/**
 * 网址提取工具，从指定列表页提取需要入库的网址
 */

namespace App\Handlers;

use Goutte\Client;
use Symfony\Component\DomCrawler\Crawler;
use App\Models\CrawlerUrl;
class CrawlerUrlsHandler
{
    protected $url, $listHtml = '', $origin_url_id, $category_id, $urlinfo, $baseUri, $article_urls = [], $crawler, $tags = '';

    public function __construct($originurl)
    {
        $url = $originurl->url;
        $this->url = $url;
        $this->baseUri = parse_url($url, PHP_URL_HOST);
        $this->urlinfo = parse_url($url);
        $this->category_id = $originurl->category_id;
        $this->origin_url_id = $originurl->id;
        $this->tags = $originurl->tags;
        $this->getHtml();
    }
    public function getHtml()
    {
        // 抓取 HTML 页面
        $http = new Client;
        return $this->crawler = $http->request('GET', $this->url);
    }
    public function add($url)
    {
        $array = parse_url($url);
        if(!isset($array['host'])) return $this->urlinfo['scheme'] . "://" . $this->baseUri . str_replace('//', '/', '/' . $url);
        else return $url;
    }
    public function getList($test = false)
    {
        switch($this->baseUri) {
            case 'www.qqyou.com':
            {
                $filter = 'ul.list-cont.left > li';
                $filtera = 'a';
                break;
            }
            case 'www.woyaogexing.com':
            {
                $filter = 'div.txList, div.txList_1, div.txList-sj';
                $filtera = 'a.imgTitle';
                break;
            }
            case 'www.qqtn.com':
            {
                $filter = 'ul.g-gxlist-imgbox > li';
                $filtera = 'a';
                break;
            }
            default:
            {
                $filter = 'ul > li';
                $filtera = 'a';
            };
        }

        // 获取匹配规则
        $filter = \App\Models\CrawlerFilter::where('origin_url_id', $this->origin_url_id)->first();
        if (!$filter) {
          return;
        }
        // dd($filter->link_filter);
        $this->crawler->filter($filter->list_filter)->each( function($node) use ($filter, $test) {

            $url = $this->add($node->filter($filter->link_filter)->attr('href'));
            $crawler_url = ['url' => $url, 'origin_url_id' => $this->origin_url_id, 'category_id' => $this->category_id, 'tags' => $this->tags];
            if (!$test) {
                // 将抓去到的详情页地址入库
                CrawlerUrl::firstOrCreate($crawler_url);
            } else {
                $this->article_urls[] = $crawler_url;
            }
        });
        if ($test) {
            $this->listHtml = $this->crawler->filter($filter->list_filter)->html();
            return ['domain' => $this->baseUri, 'filter' => $filter, 'listHtml' => $this->listHtml, 'urls' => $this->article_urls ];
        }
        return $this->article_urls;
    }
}
